using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Kontakter
{
    public partial class OptionsForm : Form
    {
        private Options options;

        public OptionsForm(Options opts)
        {
            InitializeComponent();
            CustomInitializeComponent();

            options = opts;
        }

        private void CustomInitializeComponent()
        {
            LoginIn.KeyDown += new KeyEventHandler(OnKeyDown);
            PasswordIn.KeyDown += new KeyEventHandler(OnKeyDown);

            ProxyServerIn.KeyDown += new KeyEventHandler(OnKeyDown);
            PortIn.KeyDown += new KeyEventHandler(OnKeyDown);

            ProxyUserIn.KeyDown += new KeyEventHandler(OnKeyDown);
            ProxyPasswordIn.KeyDown += new KeyEventHandler(OnKeyDown);

            UseProxyCheckBox.CheckedChanged += new EventHandler(UpdateNetworkControlsEnabledState);
            ReqAuthCheckBox.CheckedChanged += new EventHandler(UpdateNetworkControlsEnabledState);
        }

        protected override void OnCreateControl()
        {
            LoginIn.Text = options.Login;
            PasswordIn.Text = options.Password;

            ProxyServerIn.Text = options.ProxyServer;
            PortIn.Text = options.ProxyPort.ToString();

            ProxyUserIn.Text = options.ProxyLogin;
            ProxyPasswordIn.Text = options.ProxyPassword;

            UseProxyCheckBox.Checked = options.UseProxyServer;
            ReqAuthCheckBox.Checked = options.ProxyRequiresAuthorization;

            UpdateIntervalTextBox.Text = options.WaitTime.ToString();

            UpdateNetworkControlsEnabledState(this, null);

            base.OnCreateControl();
        }

        private void UpdateNetworkControlsEnabledState(object sender, EventArgs e)
        {
            ProxyServerIn.Enabled = UseProxyCheckBox.Checked;
            PortIn.Enabled = UseProxyCheckBox.Checked;

            ProxyUserIn.Enabled = UseProxyCheckBox.Checked && ReqAuthCheckBox.Checked;
            ProxyPasswordIn.Enabled = UseProxyCheckBox.Checked && ReqAuthCheckBox.Checked;

            ReqAuthCheckBox.Enabled = UseProxyCheckBox.Checked;
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SaveChanges();

                this.DialogResult = DialogResult.OK;
            }

            if (e.KeyCode == Keys.Escape)
                this.DialogResult = DialogResult.Cancel;
        }

        private void OnOkButtonClick(object sender, EventArgs e)
        {
            SaveChanges();

            this.DialogResult = DialogResult.OK;
        }

        private void OnCancelButtonClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void SaveChanges()
        {
            options.Login = LoginIn.Text;
            options.Password = PasswordIn.Text;

            options.ProxyServer = ProxyServerIn.Text;
            options.ProxyPort = int.Parse(PortIn.Text); // todo: catch exception

            options.ProxyLogin = ProxyUserIn.Text;
            options.ProxyPassword = ProxyPasswordIn.Text;

            options.UseProxyServer = UseProxyCheckBox.Checked;
            options.ProxyRequiresAuthorization = ReqAuthCheckBox.Checked;

            options.WaitTime = Int32.Parse(UpdateIntervalTextBox.Text); // todo: catch exception
        }
    }
}
